home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / gs24src.zip / GXDITHER.C < prev    next >
C/C++ Source or Header  |  1992-02-16  |  13KB  |  378 lines

  1. /* Copyright (C) 1989, 1990, 1991 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* gxdither.c */
  21. #include "gx.h"
  22. #include "gxfixed.h"
  23. #include "gxmatrix.h"
  24. #include "gzstate.h"
  25. #include "gzdevice.h"
  26. #include "gzcolor.h"
  27. #include "gzht.h"
  28.  
  29. /* 
  30.  *    Improved dithering for Ghostscript.  The underlying device imaging 
  31.  *    model supports dithering between two colors to generate intermediate
  32.  *    shades.  
  33.  *    
  34.  *    The strategy is to first see if the color is either pure white or
  35.  *    pure black.  In this case the problem is trivial.
  36.  *
  37.  *    Next, if the device has high quality colors (at least 256 values
  38.  *    per axis), we ask it to map the color directly.
  39.  *
  40.  *    Next, if the device is black and white, or the color happens
  41.  *    to be achromatic, we perform simple B/W dithering.
  42.  *    
  43.  *    Otherwise, things are a bit more complicated.  If the device 
  44.  *     supports N shades of each R, G and B independently, there are a total 
  45.  *    of N*N*N colors.  These colors form a 3-D grid in a cubical color 
  46.  *    space.  The following dithering technique works by locating the 
  47.  *    color we want in this 3-D color grid and finding the eight colors 
  48.  *     that surround it.  In the case of dithering into 8 colors with 1 
  49.  *    bit for each red, green and blue, these eight colors will always 
  50.  *    be the same.
  51.  *
  52.  *    Now we consider all possible diagonal paths between the eight colors
  53.  *    and chose the path that runs closest to our desired color in 3-D
  54.  *    color space.  There are 28 such paths.  Then we find the position
  55.  *    on the path that is closest to our color.
  56.  *
  57.  *    The search is made faster by always reflecting our color into
  58.  *    the bottom octant of the cube and comparing it to 7 paths.
  59.  *    After the best path and the best position on that path are found,
  60.  *    the results are reflected back into the original color space.
  61.  *
  62.  *    NOTE: This code has been tested for B/W and Color imaging with
  63.  *    1, 2, 3 and 8 bits per component.
  64.  *
  65.  *    --- original code by Paul Haeberli @ Silicon Graphics - 1990
  66.  *    --- extensively revised by L. Peter Deutsch, Aladdin Enterprises
  67.  */
  68.  
  69. void gx_color_load(P2(gx_device_color *, gs_state *));
  70.  
  71. #define    WEIGHT1        (unsigned long)(100)    /* 1.0             */
  72. #define    WEIGHT2        (unsigned long)(71)    /* 1/sqrt(2.0)         */
  73. #define    WEIGHT3        (unsigned long)(62)    /* 1/sqrt(3.0)+tad     */
  74.  
  75. #define    DIAG_R        (0x1)
  76. #define    DIAG_G        (0x2)
  77. #define    DIAG_B        (0x4)
  78. #define    DIAG_RG        (0x3)
  79. #define    DIAG_GB        (0x6)
  80. #define    DIAG_BR        (0x5)
  81. #define    DIAG_RGB    (0x7)
  82.  
  83. static unsigned short lum[8] = {
  84.     (0*lum_blue_weight+0*lum_green_weight+0*lum_red_weight),
  85.     (0*lum_blue_weight+0*lum_green_weight+1*lum_red_weight),
  86.     (0*lum_blue_weight+1*lum_green_weight+0*lum_red_weight),
  87.     (0*lum_blue_weight+1*lum_green_weight+1*lum_red_weight),
  88.     (1*lum_blue_weight+0*lum_green_weight+0*lum_red_weight),
  89.     (1*lum_blue_weight+0*lum_green_weight+1*lum_red_weight),
  90.     (1*lum_blue_weight+1*lum_green_weight+0*lum_red_weight),
  91.     (1*lum_blue_weight+1*lum_green_weight+1*lum_red_weight),
  92. };
  93.  
  94. /* Compute a fractional color, the correctly rounded quotient of */
  95. /* f * max_color_param / maxv. */
  96. #define _fc(f, maxv)\
  97.   (gx_color_value)(((f) * (max_color_param_long * 2) + maxv) / (maxv * 2))
  98. private gx_color_value
  99.   q0[] = { 0 },
  100.   q1[] = { 0, 0xffff },
  101.   q2[] = { 0, _fc(1,2), 0xffff },
  102.   q3[] = { 0, _fc(1,3), _fc(2,3), 0xffff },
  103.   q4[] = { 0, _fc(1,4), _fc(2,4), _fc(3,4), 0xffff },
  104.   q5[] = { 0, _fc(1,5), _fc(2,5), _fc(3,5), _fc(4,5), 0xffff },
  105.   q6[] = { 0, _fc(1,6), _fc(2,6), _fc(3,6), _fc(4,6), _fc(5,6), 0xffff },
  106.   q7[] = { 0, _fc(1,7), _fc(2,7), _fc(3,7), _fc(4,7), _fc(5,7), _fc(6,7), 0xffff };
  107. private gx_color_value _ds *color_quo[8] = { q0, q1, q2, q3, q4, q5, q6, q7 };
  108. #define fractional_color(f, maxv)\
  109.   ((maxv) <= 7 ? color_quo[maxv][f] : _fc(f, maxv))
  110.  
  111. /* Note that this routine assumes that the incoming color */
  112. /* has already been mapped through the transfer functions. */
  113. void
  114. gx_color_render(gs_color *pcolor, gx_device_color *pdevc, gs_state *pgs)
  115. {    device *pdev = pgs->device;
  116.     uint max_value;
  117.     unsigned long hsize;
  118.     gx_device *dev;
  119.     gx_color_index (*map_rgb_color)(P4(gx_device *, gx_color_value,
  120.                     gx_color_value, gx_color_value));
  121.  
  122. /* Make a special check for black and white. */
  123.     if ( pcolor->is_gray )
  124.        {    if ( pcolor->luminance == 0 )
  125.            {    pdevc->color2 = pdevc->color1 = pdev->black;
  126.             pdevc->halftone_level = 0; /* pure color */
  127.             return;
  128.            }
  129.         else if ( pcolor->luminance == max_color_param )
  130.            {    pdevc->color2 = pdevc->color1 = pdev->white;
  131.             pdevc->halftone_level = 0; /* pure color */
  132.             return;
  133.            }
  134.        }
  135.  
  136. /* get a few handy values */
  137.     dev = pdev->info;
  138.     map_rgb_color = dev->procs->map_rgb_color;
  139.     hsize = (unsigned)pgs->halftone->order_size;
  140.  
  141. /* See if we should do it in black and white. */
  142.     if ( !gx_device_has_color(dev) || pcolor->is_gray )
  143.        {    color_param lum = color_luminance(pcolor);
  144.         if ( dev->color_info.max_gray >= 31 )
  145.            {    /* Give the device a chance first. */
  146.             gx_color_index cx =
  147.                 (*map_rgb_color)(dev, lum, lum, lum);
  148.             if ( cx != gx_no_color_index )
  149.                {    pdevc->color2 = pdevc->color1 = cx;
  150.                 pdevc->halftone_level = 0;
  151.                 return;
  152.                }
  153.            }
  154.         /* No luck, must dither. */
  155.         max_value = dev->color_info.dither_gray - 1;
  156.            {    unsigned long nshades = hsize * max_value + 1;
  157.             unsigned long lx =
  158.                 (nshades * lum) / (max_color_param_long+1);
  159.             color_param l = lx / hsize;
  160.             pdevc->halftone_level = lx % hsize;
  161.             lum = fractional_color(l, max_value);
  162.             pdevc->color1 = (*map_rgb_color)(dev, lum, lum, lum);
  163.             if ( pdevc->halftone_level == 0 )
  164.                {    /* Close enough to a pure color, */
  165.                 /* no dithering needed. */
  166.                 pdevc->color2 = pdevc->color1;
  167.                }
  168.             else
  169.                {    lum = fractional_color(l+1, max_value);
  170.                 pdevc->color2 =
  171.                     (*map_rgb_color)(dev, lum, lum, lum);
  172.                }
  173.             gx_color_load(pdevc, pgs);
  174.            }
  175.         return;
  176.        }
  177.  
  178. /* If we are on a high quality RGB display, try not dithering first. */
  179.     if ( dev->color_info.max_rgb >= 31 )
  180.        {    gx_color_index cx = (*map_rgb_color)(dev, pcolor->red,
  181.                              pcolor->green,
  182.                              pcolor->blue);
  183.         if ( cx != gx_no_color_index )
  184.            {    pdevc->color2 = pdevc->color1 = cx;
  185.             pdevc->halftone_level = 0;    /* pure color */
  186.             return;
  187.            }
  188.        }
  189.     max_value = dev->color_info.dither_rgb - 1;
  190.  
  191. /* must do real color dithering */
  192.    {    color_param r, g, b;
  193.     color_param rem_r = pcolor->red;
  194.     color_param rem_g = pcolor->green;
  195.     color_param rem_b = pcolor->blue;
  196.     int adjust_r, adjust_b, adjust_g;
  197.     unsigned short amax;
  198.     unsigned long dmax;
  199.     int axisc, diagc;
  200.     unsigned short lum_invert;
  201.     unsigned long dot1, dot2, dot3;
  202.     int level;
  203.  
  204.     /* Compute the quotient and remainder of each color component */
  205.     /* with the actual number of available colors.  Avoid */
  206.     /* multiplies and divides for the common values. */
  207.     /* Note that max_color_param is all 1s, so when only a short */
  208.     /* result is needed, subtracting x*max_color_param is equivalent to */
  209.     /* just adding x.  rem_{r,g,b} are short, so we can get away */
  210.     /* with short arithmetic. */
  211.     switch ( max_value )
  212.        {
  213.     case 1:            /* 8 colors */
  214.         if ( rem_r == max_color_param ) rem_r = 0, r = 1;
  215.         else r = 0;
  216.         if ( rem_g == max_color_param ) rem_g = 0, g = 1;
  217.         else g = 0;
  218.         if ( rem_b == max_color_param ) rem_b = 0, b = 1;
  219.         else b = 0;
  220.         break;
  221.         /* When max_color_param % max_value = 0, */
  222.         /* we can do some short arithmetic. */
  223.         /* Don't bother if ints are 32 bits. */
  224. #if arch_ints_are_short
  225.     case 3:            /* 64 colors */
  226.     case 15:        /* 4096 colors */
  227.        {    const color_param q = max_color_param / max_value;
  228.         r = rem_r / q;
  229.         rem_r = rem_r * max_value + r;
  230.         g = rem_g / q;
  231.         rem_g = rem_g * max_value + g;
  232.         b = rem_b / q;
  233.         rem_b = rem_b * max_value + b;
  234.        }    break;
  235. #endif
  236.     default:
  237.        {    unsigned long want_r = (ulong)max_value * rem_r;
  238.         unsigned long want_g = (ulong)max_value * rem_g;
  239.         unsigned long want_b = (ulong)max_value * rem_b;
  240.         r = want_r / max_color_param_long;
  241.         g = want_g / max_color_param_long;
  242.         b = want_b / max_color_param_long;
  243.         rem_r = (color_param)want_r + r;
  244.         rem_g = (color_param)want_g + g;
  245.         rem_b = (color_param)want_b + b;
  246.        }
  247.        }
  248.  
  249.     /* Check for no dithering required */
  250.     if ( !(rem_r | rem_g | rem_b) )
  251.        {    pdevc->color2 = pdevc->color1 =
  252.             (*map_rgb_color)(dev, fractional_color(r, max_value),
  253.                      fractional_color(g, max_value),
  254.                      fractional_color(b, max_value));
  255.         pdevc->halftone_level = 0;
  256.         return;
  257.        }
  258. #ifdef DEBUG
  259. if ( gs_debug['c'] )
  260.    {    dprintf3("[c]rgb=%x,%x,%x -->\n",
  261.          (unsigned)pcolor->red, (unsigned)pcolor->green,
  262.          (unsigned)pcolor->blue);
  263.     dprintf6("   %x+%x,%x+%x,%x+%x -->\n",
  264.         (unsigned)r, (unsigned)rem_r, (unsigned)g, (unsigned)rem_g,
  265.         (unsigned)b, (unsigned)rem_b);
  266.    }
  267. #endif
  268.  
  269. /* flip the remainder color into the 0, 0, 0 octant */
  270.     lum_invert = 0;
  271. #define half ((color_param)(max_color_param_long>>1))
  272.     if ( rem_r > half )
  273.         rem_r = max_color_param - rem_r,
  274.           adjust_r = -1, r++, lum_invert += lum_red_weight;
  275.     else
  276.         adjust_r = 1;
  277.     if ( rem_g > half)
  278.         rem_g = max_color_param - rem_g,
  279.           adjust_g = -1, g++, lum_invert += lum_green_weight;
  280.     else
  281.         adjust_g = 1;
  282.     if ( rem_b > half )
  283.         rem_b = max_color_param - rem_b,
  284.           adjust_b = -1, b++, lum_invert += lum_blue_weight;
  285.     else
  286.         adjust_b = 1;
  287.     pdevc->color1 = (*map_rgb_color)(dev, fractional_color(r, max_value),
  288.                      fractional_color(g, max_value),
  289.                      fractional_color(b, max_value));
  290. /* 
  291.  * Dot the color with each axis to find the best one of 7;
  292.  * find the color at the end of the axis chosen.
  293.  */
  294.     if ( rem_g > rem_r )
  295.        {    if ( rem_b > rem_g )
  296.             amax = rem_b, axisc = DIAG_B;
  297.         else
  298.             amax = rem_g, axisc = DIAG_G;
  299.         if ( rem_b > rem_r )
  300.             dmax = (unsigned long)rem_g+rem_b, diagc = DIAG_GB;
  301.         else
  302.             dmax = (unsigned long)rem_r+rem_g, diagc = DIAG_RG;
  303.        }
  304.     else
  305.        {    if ( rem_b > rem_r )
  306.             amax = rem_b, axisc = DIAG_B;
  307.         else
  308.             amax = rem_r, axisc = DIAG_R;
  309.         if ( rem_b > rem_g )
  310.             dmax = (unsigned long)rem_b+rem_r, diagc = DIAG_BR;
  311.         else
  312.             dmax = (unsigned long)rem_r+rem_g, diagc = DIAG_RG;
  313.        }
  314.  
  315.     dot1 = amax*WEIGHT1;
  316.     dot2 = dmax*WEIGHT2;
  317.     dot3 = (ulong)rem_r+rem_g+rem_b;    /* rgb axis */
  318.     if ( dot1 > dot2 )
  319.        {    if ( dot3*WEIGHT3 > dot1 )
  320.             diagc = DIAG_RGB,
  321.               level = (hsize * dot3) / (3 * max_color_param_long);
  322.         else
  323.             diagc = axisc,
  324.               level = (hsize * amax) / max_color_param_long;
  325.        }
  326.     else
  327.        {    if ( dot3*WEIGHT3 > dot2 )
  328.             diagc = DIAG_RGB,
  329.               level = (hsize * dot3) / (3 * max_color_param_long);
  330.         else
  331.             level = (hsize * dmax) / (2 * max_color_param_long);
  332.        };
  333. #ifdef DEBUG
  334. if ( gs_debug['c'] )
  335.    {    dprintf6("   %x+%x,%x+%x,%x+%x;",
  336.         (unsigned)r, (unsigned)rem_r, (unsigned)g, (unsigned)rem_g,
  337.         (unsigned)b, (unsigned)rem_b);
  338.     dprintf3(" adjust=%d,%d,%d;\n",
  339.         adjust_r, adjust_g, adjust_b);
  340.    }
  341. #endif
  342.  
  343.     if ( (pdevc->halftone_level = level) == 0 )
  344.         pdevc->color2 = pdevc->color1;
  345.     else
  346.        {    gx_color_index color2;
  347. /* construct the second color, inverting back to original space if needed */
  348.         if (diagc & DIAG_R) r += adjust_r;
  349.         if (diagc & DIAG_G) g += adjust_g;
  350.         if (diagc & DIAG_B) b += adjust_b;
  351. /* get the second device color, sorting by luminance */
  352.         color2 = (*map_rgb_color)(dev, fractional_color(r, max_value),
  353.                       fractional_color(g, max_value),
  354.                       fractional_color(b, max_value));
  355. /****** THIS IS A BAD IDEA ******/
  356. #if 0
  357.         if ( lum[diagc] < lum_invert )
  358.            {    pdevc->color2 = pdevc->color1;
  359.             pdevc->color1 = color2;
  360.             pdevc->halftone_level = level = hsize - level;
  361.            }
  362.         else
  363. #endif
  364.             pdevc->color2 = color2;
  365.         gx_color_load(pdevc, pgs);
  366.        }
  367.  
  368. #ifdef DEBUG
  369. if ( gs_debug['c'] )
  370.    {    dprintf5("[c]diagc=%d; color1=%lx, color2=%lx, level=%d/%d\n",
  371.          diagc, (ulong)pdevc->color1, (ulong)pdevc->color2,
  372.          level, (unsigned)hsize);
  373.    }
  374. #endif
  375.  
  376.    }
  377. }
  378.